package com.orchard.xpz.service.pesticidesBuy.Impl;

import com.github.pagehelper.PageHelper;
import com.orchard.xpz.dao.PesticidesBuyDao;
import com.orchard.xpz.entity.pesticidesBuy.PesticidesBuy;
import com.orchard.xpz.entity.plantFood.PlantFood;
import com.orchard.xpz.service.pesticidesBuy.PesticidesBuyService;
import com.orchard.xpz.utils.PageResultData;
import com.orchard.xpz.utils.UserRuntime;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @TaskCode:
 * @description:
 * @author: minshuai.chen
 * @create: 2022/4/19 15:34
 **/
@Service
public class PesticidesBuyServiceImpl implements PesticidesBuyService {

    @Autowired
    private PesticidesBuyDao pesticidesBuyDao;

    @Override
    public PageResultData<PesticidesBuy> getPageList(PesticidesBuy pesticidesBuy) {
        PageHelper.startPage(pesticidesBuy.getPageNum(),pesticidesBuy.getPageSize());
        List<PesticidesBuy> list = pesticidesBuyDao.getList(pesticidesBuy);
        PageResultData<PesticidesBuy> pageResultData = new PageResultData<>(list, pesticidesBuy.getPageNum(), pesticidesBuy.getPageSize());
        return pageResultData;
    }

    @Override
    public boolean saveOrUpdate(PesticidesBuy pesticidesBuy) {
        if (ObjectUtils.isEmpty(pesticidesBuy.getPesticidesId())){
            pesticidesBuy.setCreateName(UserRuntime.getUser().getUserName());
            pesticidesBuy.setCreateTime(LocalDate.now());
            int i = pesticidesBuyDao.insertSelective(pesticidesBuy);
            return i > 0? true : false;
        }else {
            pesticidesBuy.setUpdateTime(LocalDate.now());
            pesticidesBuy.setUpdateName(UserRuntime.getUser().getUserName());
            int i = pesticidesBuyDao.updateByPrimaryKeySelective(pesticidesBuy);
            return i > 0? true : false;
        }
    }

    @Override
    public boolean deltePlot(String id) {
        return pesticidesBuyDao.deleteByPrimaryKey(id) > 0? true : false;
    }

    @Override
    public List<PesticidesBuy> getCountList(PesticidesBuy pesticidesBuy) {
        return pesticidesBuyDao.getCountList(pesticidesBuy);
    }
}
